Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

32943-Sugar for HierarchyId path generation #33062

Merged

Conversation

Rezakazemi890
Copy link
Contributor

@Rezakazemi890 Rezakazemi890 commented Feb 11, 2024

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Add overload for Parse method with parent parameter overload to HierarchyId.
        - Add tests to ensure that both overloads behave correctly and identically.

        Fixes #32943 
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Fixes #32943

@Rezakazemi890
Copy link
Contributor Author

Rezakazemi890 commented Feb 11, 2024

@dotnet-policy-service agree

/// <returns>A <see cref="HierarchyId" /> value.</returns>
[return: NotNullIfNotNull(nameof(parentHierarchyId))]
[return: NotNullIfNotNull(nameof(parentId))]
public static HierarchyId? Parse(HierarchyId parentHierarchyId , IReadOnlyList<int> parentId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are nulls allowed here? Also, consider making this a TryAdd if it expected to fail, but I'm not sure it should fail, since even if the HierarchyId created is not valid, that won't be known until it is saved to the database.

Copy link
Contributor Author

@Rezakazemi890 Rezakazemi890 Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what you mean; you are correct. I have fixed it in the This Commit.

//This Method can move to "SqlHierarchyId in Microsoft.SqlServer.Types", if we don't want put it in this abstraction.
private static HierarchyId GenerateHierarchyIdBasedOnParent(HierarchyId parent, IReadOnlyList<int> parentId)
{
if (parent is null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add curlies here. In general, try to follow the formatting of other cs files in the project.

Copy link
Contributor Author

@Rezakazemi890 Rezakazemi890 Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in This Commit.

/// </summary>
/// <param name="parentHierarchyId">The parent HierarchyId of node.</param>
/// <param name="parentId">The parent Id of current node. It can be more than one element if want have path like: "/1/2/3.1/", otherwise one element for have path like: "/1/2/3/".</param>
public HierarchyId(HierarchyId parentHierarchyId, IReadOnlyList<int> parentId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need for both a Parse method and a constructor?

Copy link
Contributor Author

@Rezakazemi890 Rezakazemi890 Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it because another overload of the Parse Method is used in the constructor, and I think it's not a bad idea to include it. It can be removed and I removed the constructor with this overload in This Commit

@Rezakazemi890
Copy link
Contributor Author

I attempted to simplify the new Parse overload for ease of use, and I subsequently removed the constructor with this overload in This Commit

specificPath.Append(string.Join(".", parentId));
specificPath.Append('/');

return HierarchyId.Parse(specificPath.ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this can ever return null, right? In which case the return type here should be non-nullable.

Copy link
Contributor Author

@Rezakazemi890 Rezakazemi890 Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean this method will never have a null output, it is correct. I Modified the new Parse overload output type in This commit

Also, the main overload of the Parse method that used, will not have a null output if it has a non-null input.

    [return: NotNullIfNotNull(nameof(input))]
    public static HierarchyId? Parse(string? input)
        => (HierarchyId?)SqlHierarchyId.Parse(input);

@ajcvickers ajcvickers self-assigned this Feb 28, 2024
@ajcvickers ajcvickers merged commit 13ee42c into dotnet:main Feb 28, 2024
7 checks passed
@ajcvickers
Copy link
Member

Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sugar for HierarchyId path generation
2 participants